package com.tencent.ncnnyolox.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.tencent.ncnnyolox.R;
import com.tencent.ncnnyolox.discern.Message;
import com.tencent.ncnnyolox.net.bean.response.ChatHistoryResponse;

import java.util.List;

/**
 * <pre>
 *     author : yh
 *     e-mail : xxx@xx
 *     time   : 2024/01/06
 *     desc   :
 * </pre>
 */
public class ChatRecordAdapter extends RecyclerView.Adapter<ChatRecordAdapter.ViewHolder>{
    private List<ChatHistoryResponse> mMsgList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivVoice;
        LinearLayout leftLayout;

        LinearLayout rightLayout;

        TextView leftMsg;

        TextView rightMsg;

        public ViewHolder(View view) {
            super(view);
            leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            leftMsg = (TextView) view.findViewById(R.id.left_msg);
            rightMsg = (TextView) view.findViewById(R.id.right_msg);
            //声音播放
            ivVoice = (ImageView) view.findViewById(R.id.iv_voice);


        }
    }

    public ChatRecordAdapter(List<ChatHistoryResponse> msgList) {
        this.mMsgList = msgList;
    }

    public void setData(List<ChatHistoryResponse> msgList){
        this.mMsgList=msgList;
    }

    public void addItem(ChatHistoryResponse message) {
        mMsgList.add(message);
        notifyDataSetChanged();
    }

    @Override
    public ChatRecordAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false);
        return new ChatRecordAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChatRecordAdapter.ViewHolder holder, int position) {
       ChatHistoryResponse response=mMsgList.get(position);

        if (position==0) {
            //如果收到的消息，则显示左边的消息布局，将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);

        } else  {
            //如果是发出的消息，则显示右边的消息布局，将左边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(response.getContent());
            holder.ivVoice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //调用语音
                    ((AnimationDrawable) holder.ivVoice.getDrawable()).start();

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}
